﻿using System;
using E_Commerce.Domain.Entities;
using E_Commerce.Domain.Interfaces;//引用
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using AutoMapper;//引用AutoMapper
using E_Commerce.Application.Dto;

namespace E_Commerce.Application
{
    /// <summary>
    /// 商品模块
    /// </summary>
    public class ProductService:IProductService
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        private IProductRepository _productRepository;
        private IMapper _mapper;
        public ProductService(IProductRepository productRepository,IMapper mapper)
        {
            _productRepository = productRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 显示回收站
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<Product> AgainAsync()
        {

            return _productRepository.AgainAsync();
        }
        /// <summary>
        /// 商品分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ProductTypes> CommodityAsync(int id)
        {
            return await _productRepository.CommodityAsync(id);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<int> DeleAsync(int id)
        {
            return await _productRepository.DeleAsync(id);
        }
        /// <summary>
        /// 还原
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<int> DownStatusAsync(int id)
        {
            Product product =await _productRepository.GetByIdAsync(id);
            if (product==null)
            {
                throw new InvalidOperationException("ID不存在");//提示
            }
            product.OutBox();
            return await _productRepository.UpdateAsync(product); 
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<Product> FillAsync(int id)
        {
            return await _productRepository.FillAsync(id);
        }
        /// <summary>
        /// 显示+分页+查询
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="totalCount">总条数</param>
        /// <param name="pName">商品名称</param>
        /// <returns></returns>
        public List<Product> GetProductsAsync(int pageIndex, int pageSize, out int totalCount, string pName = "")
        {
            return _productRepository.GetProductsAsync(pageIndex, pageSize, out totalCount, pName);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public async Task<int> InsertAsync(ProductDto p)
        {
            Product product = _mapper.Map<Product>(p);//automapper映射
            return await _productRepository.InsertAsync(product);
        }
        /// <summary>
        /// 商品详情（商品规格）
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ProductSpecification ProductdetailsAsync(int id)
        {
            return _productRepository.ProductdetailsAsync(id);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public async Task<int> UptAsync(Product p)
        {
            return await _productRepository.UptAsync(p);
        }
        /// <summary>
        /// 加入回收站
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> UptStatusAsync(int id)
        {
            Product p =await _productRepository.GetByIdAsync(id);
            if (p==null)
            {
                throw new InvalidOperationException("ID不存在");
            }
            p.JoinBox();
            return await _productRepository.UpdateAsync(p);
        }
    }
}
